package src.com.neusoft.nep.common;

import src.com.neusoft.nep.entity.ProvidedData;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FileUtils {
    public static Map readMapObject(String fileName){
        File file = new File(fileName);
        Map map = null;
        try {
            if (file.length() == 0) {
                map = new HashMap<>();
            } else {
                FileInputStream fis = null;
                fis = new FileInputStream(file);
                ObjectInputStream ois = new ObjectInputStream(fis);
                map = (Map) ois.readObject();
            }
        }catch (FileNotFoundException e){
            throw new RuntimeException(e);
        }catch (ClassNotFoundException e){
            throw new RuntimeException(e);
        }catch (IOException e){
            throw new RuntimeException(e);
        }
        return map;
    }
    public static void writeMapObject(String fileName, Map map){
        try {
            File file = new File(fileName);
            if (!file.exists()) {
                file.createNewFile();
            }
            FileOutputStream fos = new FileOutputStream(file);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(map);
            oos.flush();
        }catch (IOException e){
            throw new RuntimeException(e);
        }
    }
    // 新增：读取 Map<String, ProvidedData> 的方法
    public static Map<String, ProvidedData> readMapObjectx(String fileName) {
        Map<String, ProvidedData> dataMap = new HashMap<>();

        try (BufferedReader reader = Files.newBufferedReader(Paths.get(fileName))) {
            String line;
            boolean isHeader = true;
            List<String> headers = null;

            while ((line = reader.readLine()) != null) {
                if (isHeader) {
                    headers = Arrays.asList(line.split(","));
                    isHeader = false;
                    continue;
                }

                String[] values = line.split(",");
                if (values.length < 2) continue; // 至少需要 key 和一个值

                String key = values[0];
                ProvidedData data = new ProvidedData();

                // 假设文件格式: key,value1,value2,...
                // 根据 ProvidedData 类的属性进行赋值
                if (headers != null && headers.size() > 1) {
                    for (int i = 1; i < Math.min(headers.size(), values.length); i++) {
                        String header = headers.get(i);
                        String value = values[i];

                        // 根据 header 名称设置对应的属性
                        switch (header.toLowerCase()) {
                            case "aqi":
                                data.setAqi(Integer.parseInt(value));
                                break;
                            case "level":
                                data.setLevel(value);
                                break;
                            case "date":
                                data.setDate(String.valueOf(LocalDate.parse(value)));
                                break;
                            // 添加更多属性映射...
                        }
                    }
                }

                dataMap.put(key, data);
            }

            System.out.println("成功读取 " + dataMap.size() + " 条数据");

        } catch (IOException e) {
            System.err.println("文件读取异常: " + e.getMessage());
            e.printStackTrace();
        } catch (Exception e) {
            System.err.println("处理文件内容时发生未知异常: " + e.getMessage());
            e.printStackTrace();
        }

        return dataMap;
    }
}